//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LongstaffSchwartzMultiPathPricer.h"
using namespace Cephei::QL::Experimental::Mcbasket;
#include <gen/QL/Methods/Montecarlo/MultiPath.h>
#include <gen/QL/Experimental/Mcbasket/PathPayoff.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Methods::Montecarlo;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (Cephei::QL::Experimental::Mcbasket::IPathPayoff^ payoff, Cephei::Core::IVector<UInt64>^ timePositions, Cephei::Core::IVector<Cephei::QL::Termstructures::IYieldTermStructure^>^ forwardTermStructures, Cephei::QL::Math::IArray^ discounts, UInt64 polynomOrder, QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum polynomType) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPathPayoff^ _Cpayoff;
    CArray^ _Cdiscounts;
    try
    {
#ifdef HANDLE
        _phLongstaffSchwartzMultiPathPricer = NULL;
#endif
        _Cpayoff = safe_cast<CPathPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::PathPayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::PathPayoff>&> (_Cpayoff->GetShared ()); 
        timePositions->Lock();
        INativeVector<UInt64>^ _NCItimePositions = timePositions->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCtimePositions = safe_cast<CUInt64Vector^>(_NCItimePositions);
        std::vector<QuantLib::Size>& _timePositions = static_cast<std::vector<QuantLib::Size>&> (_NCtimePositions->GetReference ());
        forwardTermStructures ->Lock ();
        INativeVector<Cephei::QL::Termstructures::IYieldTermStructure^>^ _NCIforwardTermStructures = forwardTermStructures->getFeature (NativeFeature::Handle);
        CYieldTermStructureVector^ _NCforwardTermStructures = safe_cast<CYieldTermStructureVector^>(_NCIforwardTermStructures);
        std::vector<Handle<QuantLib::YieldTermStructure> >& _forwardTermStructures = static_cast<std::vector<Handle<QuantLib::YieldTermStructure> >&> (_NCforwardTermStructures->GetHandle ());
        _Cdiscounts = safe_cast<CArray^> (discounts);
        _Cdiscounts->Lock();
        QuantLib::Array& _discounts = static_cast<QuantLib::Array&> (_Cdiscounts->GetReference ()); 
        QuantLib::Size _polynomOrder = (QuantLib::Size)ValueHelper::Convert (polynomOrder); //d
        QuantLib::LsmBasisSystem::PolynomType _polynomType = (QuantLib::LsmBasisSystem::PolynomType)polynomType ;
        _ppLongstaffSchwartzMultiPathPricer = new boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer> (new QuantLib::LongstaffSchwartzMultiPathPricer ( _payoff,  _timePositions,  _forwardTermStructures,  _discounts,  _polynomOrder,  _polynomType ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (timePositions != nullptr) timePositions->Unlock();    //not optional
        if (forwardTermStructures != nullptr) forwardTermStructures->Unlock();    //not optional
        if (_Cdiscounts != nullptr) _Cdiscounts->Unlock();
    }
}
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLongstaffSchwartzMultiPathPricer = NULL;
#endif
	_ppLongstaffSchwartzMultiPathPricer = &childNative;
    
}
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (QuantLib::LongstaffSchwartzMultiPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLongstaffSchwartzMultiPathPricer = NULL;
#endif
	_ppLongstaffSchwartzMultiPathPricer = new boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer> (&childNative);
    
    _LongstaffSchwartzMultiPathPricerOwner = owner;
    
}

Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (CLongstaffSchwartzMultiPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLongstaffSchwartzMultiPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLongstaffSchwartzMultiPathPricer = new boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLongstaffSchwartzMultiPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CLongstaffSchwartzMultiPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (QuantLib::Handle<QuantLib::LongstaffSchwartzMultiPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLongstaffSchwartzMultiPathPricer = &childNative;
	_ppLongstaffSchwartzMultiPathPricer = &static_cast<boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer>>(childNative.currentLink());
    
    _LongstaffSchwartzMultiPathPricerOwner = owner;
}
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (QuantLib::Handle<QuantLib::LongstaffSchwartzMultiPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLongstaffSchwartzMultiPathPricer = &childNative;
	_ppLongstaffSchwartzMultiPathPricer = &static_cast<boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::CLongstaffSchwartzMultiPathPricer (QuantLib::LongstaffSchwartzMultiPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLongstaffSchwartzMultiPathPricer = NULL;
#endif
	_ppLongstaffSchwartzMultiPathPricer = new boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer> (new QuantLib::LongstaffSchwartzMultiPathPricer (childNative));
    
}
#endif

Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::~CLongstaffSchwartzMultiPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLongstaffSchwartzMultiPathPricer != NULL)
    {
	    delete _ppLongstaffSchwartzMultiPathPricer;
        _ppLongstaffSchwartzMultiPathPricer = NULL;
    }
}
Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::!CLongstaffSchwartzMultiPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLongstaffSchwartzMultiPathPricer != NULL)
    {
	    delete _ppLongstaffSchwartzMultiPathPricer;
    }
}
QuantLib::LongstaffSchwartzMultiPathPricer& Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::GetReference ()
{
    if (_ppLongstaffSchwartzMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppLongstaffSchwartzMultiPathPricer;
}
boost::shared_ptr<QuantLib::LongstaffSchwartzMultiPathPricer>& Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::GetShared ()
{
    if (_ppLongstaffSchwartzMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppLongstaffSchwartzMultiPathPricer;
}
QuantLib::LongstaffSchwartzMultiPathPricer* Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::GetPointer ()
{
    if (_ppLongstaffSchwartzMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppLongstaffSchwartzMultiPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LongstaffSchwartzMultiPathPricer>& Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::GetHandle ()
{
	if (_phLongstaffSchwartzMultiPathPricer == NULL)
	{
		_phLongstaffSchwartzMultiPathPricer = new Handle<QuantLib::LongstaffSchwartzMultiPathPricer> (*_ppLongstaffSchwartzMultiPathPricer);
	}
	return *_phLongstaffSchwartzMultiPathPricer;
}
#endif
bool Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::HasNative () 
{
	return (_ppLongstaffSchwartzMultiPathPricer != NULL);
}

Cephei::QL::Experimental::Mcbasket::ILongstaffSchwartzMultiPathPricer^ Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer::Calibrate::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppLongstaffSchwartzMultiPathPricer)->calibrate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Mcbasket::ILongstaffSchwartzMultiPathPricer^ Cephei::QL::Experimental::Mcbasket::CLongstaffSchwartzMultiPathPricer_Factory::Create (Cephei::QL::Experimental::Mcbasket::IPathPayoff^ payoff, Cephei::Core::IVector<UInt64>^ timePositions, Cephei::Core::IVector<Cephei::QL::Termstructures::IYieldTermStructure^>^ forwardTermStructures, Cephei::QL::Math::IArray^ discounts, UInt64 polynomOrder, QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum polynomType)
{
    return REFNEW CLongstaffSchwartzMultiPathPricer ( payoff,  timePositions,  forwardTermStructures,  discounts,  polynomOrder,  polynomType);
}
